
* ----------------------------
* preamble
* ----------------------------

capture log close
clear all
set more off
set matsize 11000
set maxvar 32767

* directories 
	cd "/nfs/sloanlab001/projects/peter_proj/DoFiles/"
	global data_dir "../WorkingData"
	global log_dir "../LogFiles"
	global output_dir "../Output"

* figures
	set scheme simplescheme
	local graphfont "Garamond"
	graph set window fontface `graphfont'
	graph set window fontfaceserif `graphfont'
	graph set eps fontface `graphfont'
	graph set eps fontfaceserif `graphfont'
	graph set ps fontface `graphfont'
	graph set ps fontfaceserif `graphfont'
	graph set ps fontfacesans `graphfont'
	graph set ps fontfacemono `graphfont'
	global GRPOPTS "legend(off)"


log using $log_dir/main_regs.log, append

global TEAMCONTROLS "x_collab lonewolf"
global x_tenurebin "i.tenure_bin"


* ----------------------------
* Table 1
* ----------------------------


* workers 
	use $data_dir/data_desc, clear

	*Number of firms and workers
	codebook companyid 
	codebook empid if rank_pr==1
	codebook empid if rank_pr==2
	codebook empid if willbepromoted==1 & rank_pr==1

	*Number of worker-months observations
	count

	*Years
	tab year

	use $data_dir/data_desc if rank_pr==1, replace
	
	*Worker rolling averages
	sum rawpresales rawcollab pay_com_mas12 pay_salary, d
	sum pay_salary if pay_salary!=0, d 

	*Probability that employee is ever promoted
	egen everpromote=max(willbepromoted), by(empid)
	keep empid everpromote
	duplicates drop
	bys empid: assert _n==1
	sum everpromote

* managers 
	use $data_dir/data_desc, clear
	bys companyid empid: egen temp=max(rank_pr)
	gen evermanager=(temp==2)
	drop temp
	tab evermanager

	codebook empid if evermanager==1
	codebook empid if evermanager==0

	keep if evermanager==1

	*first month as a worker
	capture drop temp
	gen temp=t_month if rank_pr==1
	bys companyid empid: egen lastmonth_worker=max(temp)
	drop temp

	*month of promotion (or first month as manager)
	bys companyid empid: egen temp=min(t_month) if rank_pr==2
	bys companyid empid: egen firstmo_manager=max(temp) 
	drop temp

	*internally promoted
	gen internallypromoted=(lastmonth_worker!=.)
	tab internallypromoted
	assert lastmonth_worker<firstmo_manager if internallypromoted==1

	*Change in compensation among internally promoted
	gen temp=pay_com_mas12 if t_month==lastmonth_worker
	bys companyid empid: egen prepromopay=max(temp)
	assert prepromopay!=. if internallypromoted==1
	drop temp 

	gen tem=firstmo_manager+11
	gen temp=pay_com_mas12 if t_month==tem
	bys companyid empid: egen postpromopay=max(temp)
	drop temp tem

	*Delta pay only avail for internally promoted who made it 1 year
	gen deltapay=postpromopay-prepromopay

	gen managersal=pay_salary if rank_pr==2

	*Collapse to manager level
	collapse deltapay internallypromoted lastmonth_worker firstmo_manager prepromopay postpromopay managersal, by(companyid empid)

	sum postpromopay deltapay managersal, d

* number of subordinates (incl those not promoted and those for whom we cannot calculate mva)

	use $data_dir/manager_desc, clear
	keep neweeid companyid
	duplicates drop
	ren neweeid neweeid_manager
	save $data_dir/managersample, replace

	use $data_dir/subord_afterpromotion.dta, clear
	merge m:1 companyid neweeid_manager using $data_dir/managersample
	keep if _merge==3

	egen numinteam=count(neweeid_subordinate), by(neweeid_manager)
	keep numinteam neweeid_manager companyid
	duplicates drop
	sum numinteam, d


* ----------------------------
* Figure 1
* ----------------------------

use $data_dir/data_reg.dta, clear

* Histogram option used in all panels
	local p0 plotregion(margin(b = 0)) scale(1.4)  graphregion(margin(zero )) xtitle("")

* deflating by cpi-u 
    merge m:1 t_month using $data_dir/cpiu, keepusing(cpiu2010)
        drop if _merge==2
        drop _merge
    rename rawpresales BACKUPrawpresales
    gen rawpresales=BACKUPrawpresales*cpiu2010

* panel a: RAW SALES
local out rawpresales
local xmax=6000000
local xmin=0
local p1 percent bin(50) ytitle("") ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%")
local p2 xlabel(0 "0" 2000000 "2" 4000000 "4" 6000000 "6") 
local p3 xscale(range(0 6000000)) 
twoway histogram `out' if `out'>=`xmin' & `out'<=`xmax', `p0' `p1' `p2' `p3' saving($output_dir/fig_hist_sales.gph, replace)
graph export $output_dir/fig_hist_sales.pdf, replace

* panel c: LOG(SALES)
local out x_presales_dm
local xmax=10
local xmin=-10
local p1 percent bin(50) ytitle("") ylabel(0 "0%" 5 "5%" 10 "10%" 15 "15%")
local p2  xlabel(-10 -5 0 5 10) 
local p3 xscale(range(0 10))
twoway histogram `out' if `out'>=`xmin' & `out'<=`xmax', `p0' `p1' `p2' `p3'
graph export $output_dir/fig_hist_lsales_res.pdf, replace

* panel b: RAW COLLAB
local out rawcollab
local xmax=30
local xmin=0
local p1 percent bin(50) ytitle("") ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%")
local p2 xlabel(0 10 20 30) 
local p3 xscale(range(0 30)) 
twoway histogram `out' if `out'>=`xmin' & `out'<=`xmax', `p0' `p1' `p2' `p3'
graph export $output_dir/fig_hist_collab.pdf, replace


* panel d: RES LOG(1+COLLAB)
local out x_collab_dm
local xmax=4
local xmin=-4
local p1 percent bin(50) ytitle("") ylabel(0 "0%" 5 "5%" 10 "10%" 15 "15%" 20 "20%" )
local p2  xlabel(-4 -2 0 2 4) 
local p3 xscale(range(-4 4))  
twoway histogram `out' if `out'>=`xmin' & `out'<=`xmax', `p0' `p1' `p2' `p3'
graph export $output_dir/fig_hist_l1collab_res.pdf, replace


* ----------------------------
* Table 2
* ----------------------------

use $data_dir/data_reg, clear
eststo clear

label var x_presales "Log(sales)"
label var promoterate_fm "Jackknife firm-month promotion rate"
label var x_salesrank "Team sales rank"
label var x_topsalesrank "Top sales rank"
local order x_presales promoterate_fm x_salesrank x_topsalesrank
local e1    se label replace scalars("t3 Pre-promotion controls" "t1 Firm-month FE" "t2 R-squared") 
local e2    order(`order') keep(`order') varwidth(25) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Worker is promoted", pattern(1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{2-5} ) lhs(" ")) 
local eopt `e1' `e2' `e3' `e4'

*z_willbepromoted = willbepromoted x 100, to show promotion rate in percents
*use demeaned pre-promotion sales in regression without firm-time FE
preserve
replace x_presales=x_presales_dm
qui eststo est1: reg z_willbepromoted x_presales x_collab lonewolf $x_tenurebin promoterate_fm, cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "No"
estadd local t2 `localr2'
estadd local t3 "Yes"
restore

*add firm-month FE
qui eststo est2: reghdfe z_willbepromoted x_presales x_collab lonewolf $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 `localr2'
estadd local t3 "Yes"

*Add rank
qui eststo est3: reghdfe z_willbepromoted x_presales x_salesrank x_collab lonewolf $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 `localr2'
estadd local t3 "Yes"

*Add rank + x_topsalesrank
qui eststo est4: reghdfe z_willbepromoted x_presales x_salesrank x_topsalesrank x_collab lonewolf $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 `localr2'
estadd local t3 "Yes"

esttab est1 est2 est3 est4, `eopt'
esttab est1 est2 est3 est4 using "$output_dir/tab2.tex", `eopt'



* ----------------------------
* Table 3
* ----------------------------

* manager value added, all promotions

	use $data_dir/data_reg, clear
	eststo clear

	label var x_presales_dm "Pre-promotion log(sales)"

	local order x_presales_dm
	local e1    se label replace scalars("t1 Pre-promotion controls" "t2 R-squared")
	local e2    order(`order') keep(`order') varwidth(40) obslast
	local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
	local e4    mgroups("All positions" "Promoted to different team", pattern(1 0 1 0) span prefix( \multicolumn{@span}{c}{) suffix(}) erepeat(\cline{2-3}  \cline{4-5} ) lhs("Manager Value-Added")) 
	local eopt `e1' `e2' `e3' `e4' 

	*no controls
	qui eststo est1: reg y_mva x_presales_dm if willbepromoted==1 [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "No"
	estadd local t2 `localr2'

	*add controls
	qui eststo est2: reg y_mva x_presales_dm x_collab lonewolf $x_tenurebin if willbepromoted==1 [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'

	*no controls, new team 0 cutoff
	qui eststo est3: reg y_mva x_presales_dm if willbepromoted==1 & (fracoldteam_1m==0) [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "No"
	estadd local t2 `localr2'

	*add controls, new team 0 cutoff
	qui eststo est4: reg y_mva x_presales_dm x_collab lonewolf $x_tenurebin if willbepromoted==1 & (fracoldteam_1m==0) [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'

	esttab est1 est2 est3 est4, `eopt'
	esttab est1 est2 est3 est4 using "$output_dir/tab3.tex", `eopt'


* ----------------------------
* Figure 2 
* ----------------------------

* figure 2- panel a
	use $data_dir/data_reg, clear

	local opt1 control($x_tenurebin $TEAMCONTROLS i.companytime) ytitle("") xtitle("Residualized log sales", size(vlarge))
	local opt2 ylabel(0 "0" 0.005 "0.5%" 0.010 "1.0%" 0.015 "1.5%", labsize(vlarge) nogrid gmin angle(horizontal)) 
	local opt3 graphregion(margin(tiny)) xlabel(,labsize(vlarge)) xmtick(-4 -3 -2 -1 1 2 3 4)
	local opt4 nq(20) line(lfit) mcolors(black) lcolors(black) 
	local opt `opt1' `opt2' `opt3' `opt4' 

	binscatter willbepromoted x_presales, `opt' saving($output_dir/fig2a.gph, replace)

	*graph export "$output_dir/fig2a.tif", as(tif) replace width(1200) 
	graph export "$output_dir/fig2a.pdf", replace  fontface(Garamond) 
	graph export "$output_dir/fig2a.eps", as(eps) preview(on) replace  fontface(Garamond) 
	graph save "$output_dir/fig2a.gph", replace

* figure 2- panel b
	local opt1 control($TEAMCONTROLS $x_tenurebin) ytitle("") xtitle("Residualized log sales", size(vlarge)) 
	local opt2 ylabel(-1.5 "-1.5" -1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1 "1.0" 1.5 "1.5", labsize(vlarge) nogrid gmin angle(horizontal)) 
	local opt3 graphregion(margin(tiny)) xlabel(, labsize(vlarge)) xmtick(-4 -3 -2 -1 1 2 3 4) 
	local opt4 nq(20) line(lfit) mcolors(black) lcolors(black) yline(0, lpattern(dash) lcolor(black))
	local opt `opt1' `opt2' `opt3' `opt4' 

	use $data_dir/data_reg if willbepromoted==1, clear

	binscatter y_mva x_presales_dm [aweight=wt_invvar], `opt' saving($output_dir/fig2b.gph, replace)

	*graph export "$output_dir/fig2b.tif", as(tif) replace width(1200) 
	graph export "$output_dir/fig2b.pdf", replace  fontface(Garamond) 
	graph export "$output_dir/fig2b.eps", as(eps) preview(on) replace  fontface(Garamond) 
	graph save "$output_dir/fig2b.gph", replace


* ----------------------------
* Figure 3 and Table 4
* ----------------------------

use $data_dir/data_reg, clear
eststo clear

label var willbepromoted "Promoted"

*terciles
local order willbepromoted
local e1    se label replace scalars("t1 Pre-promotion controls")
local e2    order(`order') keep(`order') varwidth(40) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Bottom Tercile" "Middle Tercile" "Top Tercile" "Ranked \# 1" "Not \#1", pattern(1 1 1 1 1) span prefix( \multicolumn{@span}{c}{) suffix(}) erepeat()) 
local eopt `e1' `e2' `e3' `e4' 

forval i=1/3 {
eststo salestercile_`i': ivregress 2sls y_mva x_collab lonewolf $x_tenurebin (willbepromoted = promoterate_fm) if pay_group3==`i' [aweight=wt_invvar], vce(cluster empid)
estadd local t1 "Yes"
}

* formatted version 
local opt1 ylabel(-1.5 "-1.5" -1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1.0 "1.0" 1.5 "1.5", labsize(vlarge) nogrid gmin) 
local opt2 xlabel(1 `""Bottom" "tercile""' 2 `""Middle" "tercile""' 3 `""Top" "tercile""', labsize(vlarge)) 
local opt3 drop(_cons x_collab lonewolf "*.tenure_bin") yline(0, lpattern(dash) lcolor(black)) 
local opt4 vertical bycoefs byopts(yrescale) xscale(outergap(2)) mcolor(black) msize(2) lcolor(black)
local opt `opt1' `opt2' `opt3' `opt4' 
        
coefplot salestercile_1 || salestercile_2 || salestercile_3, `opt' saving($output_dir/fig3a.gph, replace) 

graph export "$output_dir/fig3a.pdf", replace  fontface(Garamond) 
graph export "$output_dir/fig3a.eps", as(eps) preview(on) replace  fontface(Garamond) 
graph save "$output_dir/fig3a.gph", replace

* top rank 
forval i=0/1 {
eststo top_`i': ivregress 2sls y_mva x_collab lonewolf $x_tenurebin (willbepromoted = promoterate_fm) if x_topsalesrank==`i' [aweight=wt_invvar], vce(cluster empid)
estadd local t1 "Yes"
}

* formatted version 
local opt1 ylabel(-1.5 "-1.5" -1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1.0 "1.0" 1.5 "1.5", labsize(vlarge) nogrid gmin) 
local opt2 xlabel(1 `""Not top ranked" "in sales""' 2 `""Top ranked" "in sales""', labsize(vlarge)) 
local opt3 drop(_cons x_collab lonewolf "*.tenure_bin") yline(0, lpattern(dash) lcolor(black)) 
local opt4 vertical bycoefs byopts(yrescale) xscale(outergap(2)) mcolor(black) msize(2) lcolor(black)
local opt `opt1' `opt2' `opt3' `opt4' 
        
coefplot top_0 || top_1, `opt' saving($output_dir/fig3b.gph, replace)
graph export "$output_dir/fig3b.pdf", replace  fontface(Garamond) 
graph export "$output_dir/fig3b.eps", as(eps) preview(on) replace  fontface(Garamond) 
graph save "$output_dir/fig3b.gph", replace


label var willbepromoted "Promoted"

local order willbepromoted
local e1    se label replace scalars("t1 Pre-promotion controls")
local e2    order(`order') keep(`order') varwidth(40) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Bottom tercile" "Middle tercile" "Top tercile" "Ranked \# 1" "Not \#1", pattern(1 1 1 1 1) span prefix( \multicolumn{@span}{c}{) suffix(}) erepeat()) 
local eopt `e1' `e2' `e3' `e4' 

esttab salestercile_1 salestercile_2 salestercile_3 top_0 top_1, `eopt'
esttab salestercile_1 salestercile_2 salestercile_3 top_0 top_1 using "$tables/tab4.tex", `eopt'


* ----------------------------
* Figure 4
* ----------------------------

* figure 4, panel a
	use $data_dir/data_reg, clear

	local opt1 control(x_presales $x_tenurebin i.companytime) ytitle("") xtitle("Residualized log collaborators", size(vlarge))
	local opt2 ylabel(0 "0" 0.005 "0.5%" 0.010 "1.0%" 0.015 "1.5%", labsize(vlarge) nogrid gmin angle(horizontal)) 
	local opt3 graphregion(margin(tiny)) xlabel(-1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1.0 "1.0" 1.5 "1.5", labsize(vlarge)) 
	local opt4 nq(20) line(lfit) mcolors(black) lcolors(black) 
	local opt `opt1' `opt2' `opt3' `opt4' 
		 
	binscatter willbepromoted x_collab, `opt' saving($output_dir/fig4a.gph, replace)

	*graph export "$output_dir/fig4a.tif", as(tif) replace width(1200) 
	graph export "$output_dir/fig4a.pdf", replace  fontface(Garamond) 
	graph export "$output_dir/fig4a.eps", as(eps) preview(on) replace  fontface(Garamond) 
	graph save "$output_dir/fig4a.gph", replace

* figure 4, panel b

	use $data_dir/data_reg, clear

	local opt1 control(x_presales_dm $x_tenurebin) ytitle("") xtitle("Residualized log collaborators", size(vlarge)) 
	local opt2 ylabel(-1.5 "-1.5" -1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1 "1.0" 1.5 "1.5", labsize(vlarge) nogrid gmin angle(horizontal)) 
	local opt3 graphregion(margin(tiny)) xlabel(-1 "-1.0" -0.5 "-0.5" 0 "0.0" 0.5 "0.5" 1.0 "1.0" 1.5 "1.5", labsize(vlarge)) 
	local opt4 nq(20) line(lfit) mcolor(black) lcolor(black) yline(0, lpattern(dash) lcolor(black))
	local opt `opt1' `opt2' `opt3' `opt4' 


    binscatter y_mva x_collab_dm if willbepromoted==1 [aweight=wt_invvar], `opt' saving($output_dir/fig4b.gph, replace)
    *graph export "$output_dir/fig4b.tif", as(tif) replace width(1200) 
	graph export "$output_dir/fig4b.pdf", replace  fontface(Garamond) 
	graph export "$output_dir/fig4b.eps", as(eps) preview(on) replace  fontface(Garamond) 
	graph save "$output_dir/fig4b.gph", replace


* ----------------------------
* Table 5
* ----------------------------

use $data_dir/data_reg, clear
eststo clear

label var x_collab "Log(collaborators)" 
label var lonewolf "Lone wolf"
label var x_collab_dm "Pre-promotion log(collaborators)" 
label var pre_lonewolf "Pre-promotion lone wolf" 

gen pre_lonewolf=lonewolf

local order x_collab lonewolf x_collab_dm pre_lonewolf
local e1    se label replace scalars("t3 Pre-promotion controls" "t1 Firm-month FE" "t2 R-squared") 
local e2    order(`order') keep(`order') varwidth(40) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Worker is promoted" "Manager value added", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{2-7} ) lhs(" ")) 
local eopt `e1' `e2' `e3' `e4' 

*promotion and collab
	qui eststo est1: reghdfe z_willbepromoted x_presales x_collab $x_tenurebin, cluster(empid) absorb(companytime)
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'
	estadd local t3 "Yes"

	qui eststo est2: reghdfe z_willbepromoted x_presales lonewolf $x_tenurebin, cluster(empid) absorb(companytime)
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'
	estadd local t3 "Yes"

	qui eststo est3: reghdfe z_willbepromoted x_presales x_collab lonewolf $x_tenurebin, cluster(empid) absorb(companytime)
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'
	estadd local t3 "Yes"

*mva and collab
	qui eststo est4: reg y_mva x_collab_dm x_presales_dm $x_tenurebin if willbepromoted==1 [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'

	*TEAMPLAYER
	*no controls
	qui eststo est5: reg y_mva lonewolf x_presales_dm $x_tenurebin  if willbepromoted==1 [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "No"
	estadd local t2 `localr2'

	*with sales and tenure
	qui eststo est6: reg y_mva x_collab_dm lonewolf x_presales_dm $x_tenurebin if willbepromoted==1 [aweight=wt_invvar], r
	local localr2=round(e(r2),0.001)
	estadd local t1 "Yes"
	estadd local t2 `localr2'

	esttab est1 est2 est3 est4 est5 est6, `eopt'
	esttab est1 est2 est3 est4 est5 est6 using "$output_dir/tab5.tex", `eopt'



* ----------------------------
* Table 6
* ----------------------------

use $data_dir/data_reg if willbepromoted==1 & y_mva!=. , clear

merge m:1 companyid positionid using $data_dir/retention_cross.dta
drop if _merge==2
assert _merge==3 if willbepromoted==1 & y_mva!=.
drop _merge

eststo clear

label var x_presales_dm "Pre-promotion log(sales)"
local order x_presales_dm 
local e1    se label replace scalars("t1 Pre-promotion controls" "t3 R-squared")
local e2    order(`order') keep(`order') varwidth(40) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Initial Teamsize" "Net Change" "Fraction Joining" "Fraction Leaving" "Percentile of Leavers", pattern(1 1 1 1 1) span prefix( \multicolumn{@span}{c}{) suffix(})) 
local eopt `e1' `e2' `e3' `e4' 

eststo est0: reghdfe numinteam_firstmonth_dm x_presales_dm x_collab lonewolf $x_tenurebin, absorb(companyid) cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 "Yes"
estadd local t3 `localr2'  

eststo est1: reghdfe num_netchange_parent_dm x_presales_dm x_collab lonewolf $x_tenurebin, absorb(companyid) cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 "Yes"
estadd local t3 `localr2'  

eststo est2: reghdfe frac_join_parent_dm x_presales_dm x_collab lonewolf $x_tenurebin, absorb(companyid) cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 "Yes"
estadd local t3 `localr2'  

eststo est3: reghdfe frac_leave_parent_dm x_presales_dm x_collab lonewolf $x_tenurebin, absorb(companyid) cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 "Yes"
estadd local t3 `localr2'  

eststo est4: reghdfe pctteam_leave_parent_dm x_presales_dm x_collab lonewolf $x_tenurebin, absorb(companyid) cluster(empid)
local localr2=round(e(r2),0.001)
estadd local t1 "Yes"
estadd local t2 "Yes"
estadd local t3 `localr2'  

esttab est0 est1 est2 est3 est4, `eopt'
esttab est0 est1 est2 est3 est4 using "$output_dir/tab6.tex", `eopt'



* ----------------------------
* Table 7
* ----------------------------

use $data_dir/data_reg, clear
eststo clear

egen groupsize_cy=mean(groupsize), by(companyid year)
gen l_groupsize_cy=log(groupsize_cy)

gen groupsizesales=l_groupsize_cy*x_presales
gen groupsizecollab=l_groupsize_cy*x_collab

gen vpaysales=cy_pay_fracvar*x_presales
gen vpaycollab=cy_pay_fracvar*x_collab

label var x_presales "Log(sales)" 
label var x_collab "Log(collaborators)" 
label var groupsizesales "Log(sales) x log(mean team size)" 
label var groupsizecollab "Log(collaborators) x log(mean team size)"
label var vpaysales "Log(sales) x share variable pay" 
label var vpaycollab "Log(collaborators) x share variable pay" 

local order x_presales groupsizesales vpaysales x_collab groupsizecollab vpaycollab
local e1    se label replace scalars("t0 Pre-promotion controls" "t1 Firm-month FE" "t2 R-squared")
local e2    order(`order') keep(`order') varwidth(25) obslast
local e3    eqlabels(none) nomtitles starlevels(* 0.10 ** 0.05 *** 0.01) nonotes 
local e4    mgroups("Worker is promoted", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) lhs(" ")) 
local eopt `e1' `e2' `e3' `e4'

*BASELINE
eststo est_1: reghdfe z_willbepromoted x_presales $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t0 "Yes"
estadd local t1 "Yes"
estadd local t2 `localr2'

*TEAM SIZE X SALES INTERACTION
eststo est_2: reghdfe z_willbepromoted x_presales groupsizesales $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t0 "Yes"
estadd local t1 "Yes"
estadd local t2 `localr2'

*ADD TEAM SIZE X COLLAB INTERACTION
eststo est_3: reghdfe z_willbepromoted x_presales groupsizesales x_collab groupsizecollab $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t0 "Yes"
estadd local t1 "Yes"
estadd local t2 `localr2'

*VARIABLE PAY X SALES INTERACTION
eststo est_4: reghdfe z_willbepromoted x_presales vpaysales $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t0 "Yes"
estadd local t1 "Yes"
estadd local t2 `localr2'

*ADD VAR PAY X COLLAB INTERACTION
eststo est_5: reghdfe z_willbepromoted x_presales vpaysales x_collab vpaycollab $x_tenurebin, cluster(empid) absorb(companytime)
local localr2=round(e(r2),0.001)
estadd local t0 "Yes"
estadd local t1 "Yes"
estadd local t2 `localr2'

esttab est_1 est_2 est_3 est_4 est_5, `eopt'
esttab est_1 est_2 est_3 est_4 est_5 using "$output_dir/tab7.tex", `eopt'


* ----------------------------
* Figure 5
* ----------------------------

* save coefficients from regression of MVA on pre-managerial characteristics
use $data_dir/data_reg.dta, clear

gen promotedsamp=(willbepromoted==1 & y_mva!=.)

*basic sample splits
sum x_presales_dm [aw=wt_invvar] if promotedsamp==1, d
local p50sales=r(p50)
local p25sales=r(p25)
local p75sales=r(p75)
g above_p50_sales=x_presales_dm>`r(p50)'

reg y_mva above_p50_sales if promotedsamp==1 [aw=wt_invvar], cluster(neweeid)

*save knots from spline using just the promoted sample
mkspline sales_spline_ 3 =  x_presales_dm if promotedsamp==1, pctile displayknots
mat K=r(knots)
local knot1=K[1,1]
local knot2=K[1,2]
drop sales_spline_*

*use previous knots to create a spline over the full sample
mkspline sales_spline_1 `knot1' sales_spline_2 `knot2' sales_spline_3 = x_presales_dm, displayknots

heckman y_mva sales_spline_* x_collab_dm $x_tenurebin [pweight=wt_invvar], select(willbepromoted = promoterate_fm) 
predict m_hat, xb

sum m_hat if promotedsamp==1
replace m_hat=m_hat-r(mean)

*drop promoted workers with missing MVA
drop if y_mva==. & willbepromoted==1

***Predict managerial output for the full set of workers
joinby companyid parentpositionid t_month using $data_dir/promoted_parentpositionid, unmatched(both)
keep if _merge==3
drop _merge

gsort companyid parentpositionid t_month -m_hat
by companyid parentpositionid t_month: gen optimized_promotion=(_n<=numpromotedfromparent) 

tab optimized_promotion
tab promotedsamp

* produce figure

	local kd1 (kdensity m_hat if optimized_promotion==1 & m_hat>=-4 & m_hat<=4, lcolor(black) lwidth(medium) lpattern(solid)) 
	local kd2 (kdensity m_hat if promotedsamp==0 & m_hat>=-4 & m_hat<=4 & numpromotedfromparent>0, lcolor(black) lwidth(medium) lpattern(dash))
	local kd3 (kdensity m_hat if promotedsamp==1 & m_hat>=-4 & m_hat<=4, lcolor(black) lwidth(medthick) lpattern(shortdash)) 
	local opt1 title("") ytitle("") xtitle( "Manager value added" ) ylabel(0 "0.0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1.0 "1.0") 
	local opt2 legend(label(1 "Best potential managers") label(2 "Nonpromoted") label(3 "Promoted") cols(1)) 
	local opt3 saving( $output_dir/m_hat_density_comparison2_sameparent.gph", replace) plotregion( m(b=0) )
	local opt3 saving("$output_dir/fig5.gph", replace) 

	graph twoway `kd1' `kd2' `kd3', `opt1' `opt2' `opt3' 
	graph export "$output_dir/fig5.pdf", replace 
	graph export "$output_dir/fig5.eps", as(eps) replace

